﻿Telerik.Web.UI.RadGrid.prototype._buildFilterMenuItemList_base = Telerik.Web.UI.RadGrid.prototype._buildFilterMenuItemList;
Telerik.Web.UI.RadGrid.prototype._buildFilterMenuItemList = function(menu, filterListOptions, arg1, arg2, arg3) {
  this._buildFilterMenuItemList_base(menu, filterListOptions, "System.String", arg2, arg3);
  for (var i = 0; i < menu.get_items().get_count(); i++) {
    var item = menu.get_items().getItem(i);
    item.set_visible(true);
  }
}
Telerik.Web.UI.RadGrid.prototype._setActiveRow = function(row, _425) {
  if (this.ClientSettings && this.ClientSettings.AllowKeyboardNavigation) {
    var args = new Telerik.Web.UI.GridDataItemCancelEventArgs(this._activeRow, _425);
    this.raise_activeRowChanging(args);
    if (args.get_cancel()) {
      return;
    }
    if (this._activeRow) {
      var _427 = $find(this._activeRow.id.split("__")[0]);
      Telerik.Web.UI.Grid.ClearItemStyle(this._activeRow, _427._data._renderActiveItemStyle, _427._data._renderActiveItemStyleClass);
    }
    this._activeRow = row;
    if (row) {
      var _428 = $find(row.id.split("__")[0]);
      Telerik.Web.UI.Grid.SetItemStyle(row, _428._data._renderActiveItemStyle, _428._data._renderActiveItemStyleClass);
      Telerik.Web.UI.Grid.ScrollIntoView(row);
    }
    this.updateClientState();
    this.raise_activeRowChanged(new Telerik.Web.UI.GridDataItemEventArgs(this._activeRow, _425));
  }
}
Telerik.Web.UI.GridFilterExpression.prototype.toDynamicLinq_base = Telerik.Web.UI.GridFilterExpression.prototype.toDynamicLinq;
Telerik.Web.UI.GridFilterExpression.prototype.toDynamicLinq = function(strArg) {
  if (this.FilterFunction == 'In') {
    var ors = this.FieldValue.split(',');
    var orList = [];
    for (var or = 0; or < ors.length; or++)
      orList.push(this.FieldName + '=' + this.getDynamicLinqValue(ors[or]));
    return '(' + orList.join(' OR ') + ')';
  }
  return this.toDynamicLinq_base(strArg);
}
Telerik.Web.UI.GridTableView.prototype.set_currentPageIndex_base = Telerik.Web.UI.GridTableView.prototype.set_currentPageIndex;
Telerik.Web.UI.GridTableView.prototype.set_currentPageIndex = function(pageIndex, cancelPageCommand, extender) {
  var changePageBox = extender && extender.get_goToPageBox().get_value() != pageIndex + 1;
  this.set_currentPageIndex_base(pageIndex, cancelPageCommand || changePageBox);
  if (changePageBox) extender.get_goToPageBox().set_value(pageIndex + 1);
}
//if ((Telerik.Web.UI.RadGrid.prototype._setActiveRow + '').indexOf("if(row&&") > 0) {
//  var foo = (Telerik.Web.UI.RadGrid.prototype._setActiveRow + '').replace("if(row&&", "if(").replace("this._activeRow=row;\n", "this._activeRow=row;\nif(!row)return;\n");
//  eval("Telerik.Web.UI.RadGrid.prototype._setActiveRow = " + foo);
//}

Dimok.Web.UI.RadGridBehavior.filterFormat = function(filterFunction) {
  var gff = Telerik.Web.UI.GridFilterFunction;

  switch (filterFunction) {
    // case gff.Contains: return "*{0}*"; 
    case gff.EndsWith: return "*{0}";
    case gff.StartsWith: return "{0}*";
    case gff.EqualTo: return "={0}";
    case gff.NotEqualTo: return "<>{0}";
    case gff.LessThanOrEqualTo: return "<={0}";
    case gff.LessThan: return "<{0}";
    case gff.GreaterThanOrEqualTo: return ">={0}";
    case gff.GreaterThan: return ">{0}";
    default: return "{0}";
  }
}

Dimok.Web.UI.RadGridBehavior.filterOnEnter = function(sender, e, gridId, col, defaultFilter, dataType) {
  //Only filter on Enter (check keyCode)
  if (e.keyCode == 13) {
    event.cancelBubble = true;
    event.keyCode = 9;
    var tableView = $find(gridId).get_masterTableView();
    var gff = Telerik.Web.UI.GridFilterFunction;
    //Try to auto set the filter function
    var filter = gff.toString(gff.NoFilter); //Default

    var query = sender.value;
    if (query.endsWith("*") && query.startsWith("*")) {
      filter = gff.toString(gff.Contains);
      query = query.substr(1, query.length - 2);
    } else if (query.startsWith("*")) {
      filter = gff.toString(gff.EndsWith);
      query = query.substr(1, query.length - 1);
    } else if (query.endsWith("*")) {
      filter = gff.toString(gff.StartsWith);
      query = query.substr(0, query.length - 1);
    } else if (query.startsWith("=")) {
      filter = gff.toString(gff.EqualTo);
      query = query.substr(1, query.length - 1);
    } else if (query.startsWith("<>")) {
      filter = gff.toString(gff.NotEqualTo);
      query = query.substr(2, query.length - 2);
    } else if (query.startsWith("<=")) {
      filter = gff.toString(gff.LessThanOrEqualTo);
      query = query.substr(2, query.length - 2);
    } else if (query.startsWith("<")) {
      filter = gff.toString(gff.LessThan);
      query = query.substr(1, query.length - 1);
    } else if (query.startsWith(">=")) {
      filter = gff.toString(gff.GreaterThanOrEqualTo);
      query = query.substr(2, query.length - 2);
    } else if (query.startsWith(">")) {
      filter = gff.toString(gff.GreaterThan);
      query = query.substr(1, query.length - 1);
    } else if (query.startsWith("><")) {
      filter = gff.toString(gff.Between);
      query = query.substr(2, query.length - 2);
    } else if (query.match(/\S+\s\S+/)) {
      filter = gff.toString(gff.Between);
    } else if (query.match(/.+,.+/)) {
      filter = gff.toString(gff.In);
    } else if (query) filter = defaultFilter;

    //Execute filter
    tableView.filter(col, query, filter);
  }
}
// ********************************]

Dimok.Web.UI.RadGridBehavior.cleanColumns = function(grid) {
  for (var c = 0, cc = this.get_masterTableView().get_columns(); c < cc.length; c++)
    Dimok.Web.UI.RadGridBehavior.cleanColumn(cc[c]);
}
Dimok.Web.UI.RadGridBehavior.cleanColumn = function(column) {
  if (!column) return;
  for (var inputs = column.get_element().all.tags("INPUT"); inputs.length > 2; )
    inputs[0].parentElement.removeChild(inputs[0]);

}